﻿layui.use(['form', 'layer', 'table', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#testPageList',
        url: '/TestPage/LoadData/',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 10,
        id: "testPageListTable",
        cols: [[
            { type: "checkbox", fixed: "left", width: 50 },
            //{ field: "Id", title: 'Id', width: 50, align: "center" },
            { field: 'Name', title: '试卷名', minWidth: 50, align: "center" },
            { field: 'LimitMin', title: '限时时间', minWidth: 50, align: "center" },
            { field: 'LimitTimes', title: '限时次数', minWidth: 80, align: "center" },
            { field: 'TotalScore', title: '总分', minWidth: 100, align: "center" },
            { field: 'PassScore', title: '合格分', minWidth: 80, align: 'center' },
            { title: '操作', minWidth: 80, templet: '#testPageListBar', fixed: "right", align: "center" }
        ]]
    });

    $(".search_btn").on("click", function () {
        table.reload("testPageListTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                key: $(".searchVal").val()  //搜索的关键字
            }
        });
    });

    //添加试卷
    function addTestPage(edit) {
        var tit = "添加试卷";
        if (edit) {
            tit = "编辑试卷";
        }
        var index = layui.layer.open({
            title: tit,
            type: 2,
            anim: 1,
            area: ['800px', '100%'],
            content: "/TestPage/AddOrModify/",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                if (edit) {
                    body.find("#Id").val(edit.Id);
                    body.find(".TestPageName").val(edit.Name);
                    body.find(".LimitTime").val(edit.LimitMin);
                    body.find(".LimitCount").val(edit.LimitTimes);
                    body.find(".TotalScore").val(edit.TotalScore);
                    body.find(".PassScore").val(edit.PassScore);
                    form.render();
                }
            }
        });
    }
    $(".addTestPage_btn").click(function () {
        addTestPage();
    });

    //批量删除
    //$(".delAll_btn").click(function () {
    //    var checkStatus = table.checkStatus('managerListTable'),
    //        data = checkStatus.data,
    //        managerId = [];
    //    if (data.length > 0) {
    //        for (var i in data) {
    //            managerId.push(data[i].Id);
    //        }
    //        layer.confirm('确定删除选中的用户？', { icon: 3, title: '提示信息' }, function (index) {
    //            //获取防伪标记
    //            del(managerId);
    //        });
    //    } else {
    //        layer.msg("请选择需要删除的用户");
    //    }
    //});

    //列表操作
    table.on('tool(testPageList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;

        if (layEvent === 'edit') { //编辑
            addTestPage(data);
        } else if (layEvent === 'del') { //删除
            testPageId = [];
            testPageId.push(data.Id);
            layer.confirm('确定删除此试卷？', { icon: 3, title: '提示信息' }, function (index) {
                del(testPageId);
            });
        }
    });

    function del(testPageId) {
        $.ajax({
            type: 'POST',
            url: '/TestPage/Delete/',
            data: { testPageId: testPageId },
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-Gerald": $("input[name='AntiforgeryKey_Gerald']").val()
            },
            success: function (data) {//res为相应体,function为回调函数
                layer.msg(data.ResultMsg, {
                    time: 2000 //20s后自动关闭
                }, function () {
                    tableIns.reload();
                    layer.close(index);
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }
});
